package com.ruoyi.pims.controller;

import java.util.List;

import com.ruoyi.common.utils.ShiroUtils;
import com.ruoyi.pims.service.IFinAccountService;
import com.ruoyi.pims.service.IFinCategoryService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.pims.domain.FinTransaction;
import com.ruoyi.pims.service.IFinTransactionService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import org.springframework.web.util.pattern.PathPattern;

/**
 * 收支记录Controller
 * 
 * @author XiFeng
 * @date 2025-05-26
 */
@Controller
@RequestMapping("/pims/transaction")
public class FinTransactionController extends BaseController
{
    private String prefix = "pims/transaction";

    @Autowired
    private IFinTransactionService finTransactionService;

    @Autowired
    private IFinCategoryService finCategoryService;

    @Autowired
    private IFinAccountService finAccountService;

    @RequiresPermissions("pims:transaction:view")
    @GetMapping()
    public String transaction()
    {
        return prefix + "/transaction";
    }

    /**
     * 查询收支记录列表
     */
    @RequiresPermissions("pims:transaction:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(FinTransaction finTransaction)
    {
        startPage();
        List<FinTransaction> list = finTransactionService.selectFinTransactionList(finTransaction);
        return getDataTable(list);
    }

    /**
     * 导出收支记录列表
     */
    @RequiresPermissions("pims:transaction:export")
    @Log(title = "收支记录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(FinTransaction finTransaction)
    {
        List<FinTransaction> list = finTransactionService.selectFinTransactionList(finTransaction);
        ExcelUtil<FinTransaction> util = new ExcelUtil<FinTransaction>(FinTransaction.class);
        return util.exportExcel(list, "收支记录数据");
    }

    /**
     * 新增收支记录
     */
    @RequiresPermissions("pims:transaction:add")
    @GetMapping("/add")
    public String add(ModelMap mmap)
    {
        mmap.put("userString", ShiroUtils.getSysUser().toUserString());
        mmap.put("categories", finCategoryService.selectFinCategoryList(null));
        mmap.put("accounts", finAccountService.selectFinAccountList(null));
        return prefix + "/add";
    }

    /**
     * 新增保存收支记录
     */
    @RequiresPermissions("pims:transaction:add")
    @Log(title = "收支记录", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(FinTransaction finTransaction)
    {
        finTransaction.setUserId(ShiroUtils.getUserId());
        return toAjax(finTransactionService.insertFinTransaction(finTransaction));
    }

    /**
     * 修改收支记录
     */
    @RequiresPermissions("pims:transaction:edit")
    @GetMapping("/edit/{transactionId}")
    public String edit(@PathVariable("transactionId") Long transactionId, ModelMap mmap)
    {
        FinTransaction finTransaction = finTransactionService.selectFinTransactionByTransactionId(transactionId);
        finTransaction.setUserId(ShiroUtils.getUserId());
        mmap.put("finTransaction", finTransaction);
        mmap.put("userString", ShiroUtils.getSysUser().toUserString());
        mmap.put("categories", finCategoryService.selectFinCategoryList(null));
        mmap.put("accounts", finAccountService.selectFinAccountList(null));
        return prefix + "/edit";
    }

    /**
     * 修改保存收支记录
     */
    @RequiresPermissions("pims:transaction:edit")
    @Log(title = "收支记录", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(FinTransaction finTransaction)
    {
        return toAjax(finTransactionService.updateFinTransaction(finTransaction));
    }

    /**
     * 删除收支记录
     */
    @RequiresPermissions("pims:transaction:remove")
    @Log(title = "收支记录", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(finTransactionService.deleteFinTransactionByTransactionIds(ids));
    }
}
